var express = require('express');
var router = express.Router();
var _ = require('underscore');

/* GET users listing. */
router.get('/guild/guild_shop/search', function(req, res, next) {
  // res.setHeader('Access-Control-Allow-Origin', '*');
  //res.send('../ajax.html');
  res.jsonp({
		code: 0,
		data: {
			keywords:'礼包',
		    list:[
			    {
					gift_id:1,
			    	gift_icon:'http://ttapk.qiniucdn.com/icon/237784.jpg',
			    	gid:1,
			    	game_name:'不良人',
			    	icon:'http://ttapk.qiniucdn.com/icon/237784.jpg',
			    	type:1,
			    	source:1,
			    	gift_name:'不良人之新手大礼包',
			    	remained:65,
			    	total:100,
			    	currency:2,
			    	price:0,
			    	status:1,
			    	receive:1,
			    	need_approve:1,
			    	start_time:0,
			    	end_time:0,
			    	desc:0,
			    	usage_method:'礼包内容',
			    	user_status:1,
			    	period:'2016.8.24~2017.9.24',
			    	start_time:144545478,
		    		end_time:1445454745,
			    },
			    {
					gift_id:2,
			    	gift_icon:'http://ttapk.qiniucdn.com/icon/237784.jpg',
			    	gid:2,
			    	game_name:'不良人2',
			    	icon:'http://ttapk.qiniucdn.com/icon/237784.jpg',
			    	type:1,
			    	source:2,
			    	gift_name:'不良人之新手大礼包',
			    	remained:18,
			    	total:100,
			    	currency:1,
			    	price:0,
			    	status:2,
			    	receive:0,
			    	need_approve:0,
			    	start_time:0,
			    	end_time:0,
			    	desc:0,
			    	usage_method:'礼包内容',
			    	period:'2016.8.24~2017.9.24',
			    	start_time:144545478,
		    		end_time:1445454745,
			    },
			     {
					gift_id:3,
			    	gift_icon:'http://ttapk.qiniucdn.com/icon/237784.jpg',
			    	gid:3,
			    	game_name:'不良人3',
			    	icon:'http://ttapk.qiniucdn.com/icon/237784.jpg',
			    	type:1,
			    	source:2,
			    	gift_name:'不良人之新手大礼包',
			    	remained:18,
			    	total:200,
			    	currency:1,
			    	price:0,
			    	status:3,
			    	receive:0,
			    	need_approve:0,
			    	start_time:0,
			    	end_time:0,
			    	desc:0,
			    	usage_method:'礼包内容',
			    	period:'2016.8.24~2017.9.24',
			    	start_time:144545478,
		    		end_time:1445454745,
			    },
			     {
					gift_id:4,
			    	gift_icon:'http://ttapk.qiniucdn.com/icon/237784.jpg',
			    	gid:3,
			    	game_name:'不良人3',
			    	icon:'http://ttapk.qiniucdn.com/icon/237784.jpg',
			    	type:1,
			    	source:2,
			    	gift_name:'不良人之新手大礼包',
			    	remained:18,
			    	total:200,
			    	currency:1,
			    	price:0,
			    	status:3,
			    	receive:0,
			    	need_approve:0,
			    	start_time:0,
			    	end_time:0,
			    	desc:0,
			    	usage_method:'礼包内容',
			    	period:'2016.8.24~2017.9.24',
			    	start_time:144545478,
		    		end_time:1445454745,
			    },
			     {
					gift_id:5,
			    	gift_icon:'http://ttapk.qiniucdn.com/icon/237784.jpg',
			    	gid:3,
			    	game_name:'不良人3',
			    	icon:'http://ttapk.qiniucdn.com/icon/237784.jpg',
			    	type:1,
			    	source:2,
			    	gift_name:'不良人之新手大礼包',
			    	remained:18,
			    	total:200,
			    	currency:1,
			    	price:0,
			    	status:3,
			    	receive:0,
			    	need_approve:0,
			    	start_time:0,
			    	end_time:0,
			    	desc:0,
			    	usage_method:'礼包内容',
			    	period:'2016.8.24~2017.9.24',
			    	start_time:144545478,
		    		end_time:1445454745,
			    },
			     {
					gift_id:6,
			    	gift_icon:'http://ttapk.qiniucdn.com/icon/237784.jpg',
			    	gid:3,
			    	game_name:'不良人3',
			    	icon:'http://ttapk.qiniucdn.com/icon/237784.jpg',
			    	type:1,
			    	source:2,
			    	gift_name:'不良人之新手大礼包',
			    	remained:18,
			    	total:200,
			    	currency:1,
			    	price:0,
			    	status:3,
			    	receive:0,
			    	need_approve:0,
			    	start_time:0,
			    	end_time:0,
			    	desc:0,
			    	usage_method:'礼包内容',
			    	period:'2016.8.24~2017.9.24',
			    	start_time:144545478,
		    		end_time:1445454745,
			    },
			     {
					gift_id:7,
			    	gift_icon:'http://ttapk.qiniucdn.com/icon/237784.jpg',
			    	gid:3,
			    	game_name:'不良人5',
			    	icon:'http://ttapk.qiniucdn.com/icon/237784.jpg',
			    	type:1,
			    	source:2,
			    	gift_name:'不良人之新手大礼包',
			    	remained:18,
			    	total:200,
			    	currency:1,
			    	price:0,
			    	status:3,
			    	receive:0,
			    	need_approve:0,
			    	start_time:0,
			    	end_time:0,
			    	desc:0,
			    	usage_method:'礼包内容',
			    	period:'2016.8.24~2017.9.24',
			    	start_time:144545478,
		    		end_time:1445454745,
			    },
		    ],
		    total:6,
		    server_time:new Date().getTime()
		},
	    msg: "OK"
   });  
});

/* GET users listing. */
router.get('/guild/guild_shop/init', function(req, res, next) {
  res.setHeader('Access-Control-Allow-Origin', '*');
  let query = req.query

  let partermter = {
    code: 0,
    data: {
        gift_total: [
            {
                source: 1,
                total: 7,
                type: 0
            },
            {
                source: 1,
                total: 0,
                type: 1
            },
            {
                source: 1,
                total: 0,
                type: 2
            },
            {
                source: 1,
                total: 0,
                type: 3
            },
            {
                source: 1,
                total: 0,
                type: 4
            },
            {
                source: 1,
                total: 0,
                type: 5
            },
            {
                source: 1,
                total: 4,
                type: 6
            },
            {
                source: 2,
                total: 3,
                type: 0
            }
        ],
        list: [
            {
                currency: 2,
                desc: "test desc",
                end_time: 0,
                game_name: "征途",
                gid: 114,
                gift_icon: "icon url",
                gift_id: 2,
                gift_name: "test p1",
                gift_total: 1,
                icon: "http://p16.qhimg.com/t0113873c7748b058a9.png",
                need_approve: 0,
                package_name: "com.joym.armorhero.sj360",
                price: 4,
                remained: 5,
                source: 1,
                start_time: 0,
                status: 1,
                total: 6,
                type: 6,
                usage_method: "test usage",
                user_status: 1
            },
            {
                currency: 2,
                desc: "test desc",
                end_time: 0,
                game_name: "征途",
                gid: 23142,
                gift_icon: "icon url",
                gift_id: 4,
                gift_name: "test p2",
                gift_total: 2,
                icon: "http://p18.qhimg.com/t01410045835dde3a86.png",
                need_approve: 0,
                package_name: "com.oodavid.moleculematch",
                price: 2,
                remained: 6,
                source: 2,
                start_time: 0,
                status: 1,
                total: 6,
                type: 6,
                usage_method: "test usage",
                user_status: 1
            },
            {
                currency: 2,
                desc: "test desc",
                end_time: 0,
                game_name: "征途",
                gid: 23142,
                gift_icon: "icon url",
                gift_id: 5,
                gift_name: "test",
                gift_total: 2,
                icon: "http://p18.qhimg.com/t01410045835dde3a86.png",
                need_approve: 0,
                package_name: "com.oodavid.moleculematch",
                price: 2,
                remained: 6,
                source: 1,
                start_time: 0,
                status: 1,
                total: 6,
                type: 6,
                usage_method: "test usage",
                user_status: 1
            },
            {
                currency: 2,
                desc: "test desc",
                end_time: 0,
                game_name: "征途",
                gid: 23112,
                gift_icon: "icon url",
                gift_id: 7,
                gift_name: "ssa",
                gift_total: 2,
                icon: "http://p18.qhimg.com/t01410045835dde3a86.png",
                need_approve: 0,
                package_name: "com.orgfree.gate11.cfrebound",
                price: 1,
                remained: 6,
                source: 1,
                start_time: 0,
                status: 1,
                total: 6,
                type: 6,
                usage_method: "test usage",
                user_status: 1
            },
            {
                currency: 2,
                desc: "test desc",
                end_time: 0,
                game_name: "征途",
                gid: 23112,
                gift_icon: "icon url",
                gift_id: 8,
                gift_name: "ssa111",
                gift_total: 2,
                icon: "http://p18.qhimg.com/t01410045835dde3a86.png",
                need_approve: 1,
                package_name: "com.orgfree.gate11.cfrebound",
                price: 2,
                remained: 3,
                source: 1,
                start_time: 0,
                status: 1,
                total: 7,
                type: 6,
                usage_method: "test usage",
                user_status: 2
            },
            {
                currency: 3,
                desc: "神神叨叨",
                end_time: 1477670400,
                game_name: "征途",
                gid: 200080,
                gift_icon: "http://p15.qhimg.com/t016d671e46b481fa38.png",
                gift_id: 10,
                gift_name: "说说",
                gift_total: 1,
                icon: "http://p15.qhimg.com/t016d671e46b481fa38.png",
                need_approve: 0,
                package_name: "com.snailgame.panda.qihoo360",
                price: 2,
                remained: 0,
                source: 2,
                start_time: 1477238400,
                status: 2,
                total: 1,
                type: 2,
                usage_method: "点点滴滴",
                user_status: 2
            },
            {
                currency: 3,
                desc: "到底点点滴滴",
                end_time: 1477670400,
                game_name: "神魔",
                gid: 3,
                gift_icon: "http://ttapk.qiniucdn.com/icon/3.jpg?imageView2/1/w/120/h/120",
                gift_id: 11,
                gift_name: "什么神魔",
                gift_total: 1,
                icon: "http://ttapk.qiniucdn.com/icon/3.jpg?imageView2/1/w/120/h/120",
                need_approve: 0,
                package_name: "com.yinhan.shenmo.qihoo",
                price: 13,
                remained: 0,
                source: 2,
                start_time: 1477238400,
                status: 2,
                total: 1,
                type: 2,
                usage_method: "少时诵诗书是什么意思",
                user_status: 2
            }
        ],
        server_time: new Date().getTime()
    },
    msg: "OK"
  }; 

  let debug = query.debug
  let page = query.page
  let page_num = query.page_num*5
  let type = query.type 
  let Things = page ? page*page_num :(100*page_num)
  let path = []

  for (var i = 0; i < Things/2; i++) {
  	 path.push(i)
  }
  for (var i = 0; i < Things; i++) {
  	  let gift_info =  {
		currency: 2,
		desc: "test desc",
		end_time: 0,
		game_name: "征途",
		gid: 114,
		gift_icon: "icon url",
		gift_id: 2,
		gift_name: "test p1",
		gift_total: 1,
		icon: "http://p16.qhimg.com/t0113873c7748b058a9.png",
		need_approve: 0,
		package_name: "com.joym.armorhero.sj360",
		price: 4,
		remained: 5,
		source: 1,
		start_time: 0,
		status: 1,
		total: 6,
		type: 6,
		usage_method: "test usage",
		user_status: 1
    };

    gift_info.game_name = _.sample(['逗逼','不良人','年会抽奖','抽中i7','我去你大爷','展护卫']);
    gift_info.status = _.sample([1, 2, 3]); //随机来源
    gift_info.user_status = _.sample([0,1, 2, 3,4]); //随机来源
    gift_info.receive = _.sample([0, 2]); //随机来源
    gift_info.source = _.sample([1, 2]); //随机来源
    gift_info.type = _.sample([1, 2, 3, 4, 5, 6]); //随机类型 识别传入的type
  	gift_info.gift_id = parseInt(i*Math.random()); //随机gift_id
  	gift_info.gid = _.sample(path); //随机数组大小
  	gift_info.price = _.sample(path); //随机数组大小

  	partermter.data.list.push(gift_info) //插入随机数据
  	partermter.data.total = parseInt(i*Math.random()*5).toFixed(0)
  	partermter.data.gift_total = parseInt(i*Math.random()*5).toFixed(0)
  }

  console.log(query)

  if(debug == 1){
  	res.json(partermter); 
  }else{
  	res.jsonp(partermter); 
  }

});

/* GET users listing. */
router.get('/user/init', function(req, res, next) {
  // res.setHeader('Access-Control-Allow-Origin', '*');
  //res.send('../ajax.html');
  res.jsonp({
		code: 0,
		data: {
			user:{
				uid:0,
				account:0,
				head:0,
				name:0,
				contribution:0,
				consume:200,
				diamond:2000,
				guild_id:0909,
				guild_title:'公会之花',
				level:1,
				guild_identity:1,
				guild_consume:200
			},
		    server_time:new Date().getTime()
		},
	    msg: "OK"
   });  
});

/* GET users listing. */
router.get('/guild/guild_shop/fetch_gift', function(req, res, next) {
  // res.setHeader('Access-Control-Allow-Origin', '*');
  //res.send('../ajax.html');
  res.jsonp({
		code: 0,
		data: {
			receive:0,
			consume:122,
			record:{
				id:0,
				type:2,
				gift_list:[
					{
						content:'menggege'
					},
					{
						content:'45878798xx4654'
					},
				]
			},
			info:{
				gift_id:1,
		    	gift_icon:'http://ttapk.qiniucdn.com/icon/237784.jpg',
		    	gid:1245,
		    	game_name:'不良人',
		    	icon:'http://ttapk.qiniucdn.com/icon/237784.jpg',
		    	type:1,
		    	source:1,
		    	gift_name:'不良人之新手大礼包',
		    	remained:65,
		    	total:2000,
		    	currency:2,
		    	price:0,
		    	status:1,
		    	receive:0,
		    	user_status:_.sample([1, 2, 3, 4, 5, 6]),
		    	need_approve:0,
		    	desc:0,
		    	user_status:2,
		    	usage_method:'礼包内容',
		    	period:'2016.8.24~2017.9.24',
		    	start_time:144545478,
		    	end_time:1445454745,
			},
		    server_time:new Date().getTime()
		},
	    msg: "OK"
   });  
});

/* GET users listing. */
router.get('/guild/guild_shop/fetch_record', function(req, res, next) {
  // res.setHeader('Access-Control-Allow-Origin', '*');
  //res.send('../ajax.html');
  let obj = {
		code: 0,
		data: {
			gift_id:21,
			// record:{
			// 	type:1,
			// 	id:1,
			// 	gift_list:[
			// 		{
			// 			content:'menggege'
			// 		},
			// 		// {
			// 		// 	content:'43573457xx23232'
			// 		// }
			// 	]
			// },
			list:[
				// {
				// 	user:{
				// 		id:0,
				// 		icon:'',
				// 		uid:1212,
				// 		name:'你是逗逼',
				// 		guild_identity:'前端大虾米',
				// 		member_level:'1',
				// 		guild_title:'121'
				// 	},
				// 	receive_time:new Date().getTime()/1000
				// },
				// {
				// 	user:{
				// 		id:1,
				// 		icon:'',
				// 		uid:1212,
				// 		name:'你是逗逼',
				// 		guild_identity:'前端大虾米',
				// 		member_level:'2',
				// 		guild_title:'121'
				// 	},
				// 	receive_time:new Date().getTime()/1000
				// },
				// {
				// 	user:{
				// 		id:2,
				// 		icon:'',
				// 		uid:1212,
				// 		name:'你是逗逼',
				// 		guild_identity:'前端大虾米',
				// 		member_level:'3',
				// 		guild_title:'121'
				// 	},
				// 	receive_time:new Date().getTime()/1000
				// },
				// {
				// 	user:{
				// 		id:3,
				// 		icon:'',
				// 		uid:1212,
				// 		name:'你是逗逼',
				// 		guild_identity:'前端大虾米',
				// 		member_level:'4',
				// 		guild_title:'121'
				// 	},
				// 	receive_time:new Date().getTime()/1000
				// },
			],
			total:40,
		    server_time:new Date().getTime()
		},
	    msg: "OK"
   }
  let item
	for (var i = 0; i < 30; i++) {
		item = {
		    user:{
				id:0,
				icon:'',
				uid:1212,
				name:'你是逗逼',
				guild_identity:'前端大虾米',
				member_level:'1',
				guild_title:'121'
			},
			receive_time:new Date().getTime()/1000
		};
		item.user.uid = i
		item.user.id =i
		obj.data.list.push(item)
	}
  res.jsonp(obj);  
});

module.exports = router;
